/*
 * MemberFrame.java
 *
 * Created on __DATE__, __TIME__
 */

package com.insigma.yanwenlong.bookkeeping.gui;

import java.sql.SQLException;
import java.util.List;

import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

import com.insigma.yanwenlong.bookkeeping.dao.CommonDao;
import com.insigma.yanwenlong.bookkeeping.pojo.Member;
import com.insigma.yanwenlong.bookkeeping.service.MemberService;
import com.insigma.yanwenlong.bookkeeping.service.MemberServiceImpl;

/**
 *
 * @author  __USER__
 */
public class MemberFrame extends javax.swing.JFrame {
	private MemberService memberService = new MemberServiceImpl();
	private DefaultTableModel dtm;

	/** Creates new form MemberFrame */
	public MemberFrame() {
		this.setTitle("成员管理");
		initComponents();
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		this.setLocationRelativeTo(null);
		//初始化成员
		initMember();
	}

	private void initMember() {
		try {
			List<Member> members = memberService.findallMembers();
			dtm = (DefaultTableModel) tblMember.getModel();
			for (Member member : members) {
				dtm.addRow(new Object[] { member.getId(), member.getName() });
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		jScrollPane1 = new javax.swing.JScrollPane();
		tblMember = new javax.swing.JTable();
		btnAddMember = new javax.swing.JButton();
		btnDeleteMember = new javax.swing.JButton();
		btnResetMember = new javax.swing.JButton();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

		tblMember.setModel(new javax.swing.table.DefaultTableModel(
				new Object[][] {

				}, new String[] { "成员编号", "成员名称" }) {
			boolean[] canEdit = new boolean[] { false, false };

			public boolean isCellEditable(int rowIndex, int columnIndex) {
				return canEdit[columnIndex];
			}
		});
		jScrollPane1.setViewportView(tblMember);

		btnAddMember.setText("\u65b0\u589e\u6210\u5458");
		btnAddMember.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				btnAddMemberActionPerformed(evt);
			}
		});

		btnDeleteMember.setText("\u5220\u9664\u6210\u5458");
		btnDeleteMember.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				btnDeleteMemberActionPerformed(evt);
			}
		});

		btnResetMember.setText("\u5237\u65b0");
		btnResetMember.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				btnResetMemberActionPerformed(evt);
			}
		});

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout
				.setHorizontalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addComponent(btnAddMember)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
										.addComponent(btnDeleteMember).addGap(
												18, 18, 18).addComponent(
												btnResetMember)).addComponent(
								jScrollPane1,
								javax.swing.GroupLayout.PREFERRED_SIZE, 325,
								javax.swing.GroupLayout.PREFERRED_SIZE));
		layout.setVerticalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				layout.createSequentialGroup().addComponent(jScrollPane1,
						javax.swing.GroupLayout.PREFERRED_SIZE, 200,
						javax.swing.GroupLayout.PREFERRED_SIZE).addGap(18, 18,
						18).addGroup(
						layout.createParallelGroup(
								javax.swing.GroupLayout.Alignment.BASELINE)
								.addComponent(btnAddMember).addComponent(
										btnDeleteMember).addComponent(
										btnResetMember)).addContainerGap(29,
						Short.MAX_VALUE)));

		pack();
	}// </editor-fold>
	//GEN-END:initComponents
	
	//点击删除成员
	private void btnDeleteMemberActionPerformed(java.awt.event.ActionEvent evt) {
		if(tblMember.getSelectedRow() < 0) {
			JOptionPane.showMessageDialog(this, "你没有选择要删除的行");
		}else {
			int[] rowCount = tblMember.getSelectedRows();
			int memberId;
			for (int i : rowCount) {
				memberId = (Integer)tblMember.getValueAt(i, 0);
				CommonDao cd = new CommonDao();
				cd.deleteBymemberId(memberId);
			}
		}
	}

	//点击新增成员
	private void btnAddMemberActionPerformed(java.awt.event.ActionEvent evt) {
		new AddMember().setVisible(true);
	}

	//点击刷新
	private void btnResetMemberActionPerformed(java.awt.event.ActionEvent evt) {
		dtm.setRowCount(0);
		initMember();
	}

	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new MemberFrame().setVisible(true);
			}
		});
	}

	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JButton btnAddMember;
	private javax.swing.JButton btnDeleteMember;
	private javax.swing.JButton btnResetMember;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JTable tblMember;
	// End of variables declaration//GEN-END:variables

}